package com.zisu.aradvertising.service.impl;

import com.zisu.aradvertising.pojo.Creater;
import com.zisu.aradvertising.pojo.Sign;
import com.zisu.aradvertising.repository.DaoCreater;
import com.zisu.aradvertising.repository.DaoSign;
import com.zisu.aradvertising.service.CreaterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CreaterServiceImpl implements CreaterService {
    //实例化抽象类
    @Resource
    private DaoCreater daoCreater;
    @Resource
    private DaoSign daoSign;

    //实现账号密码登录业务逻辑
    @Override
    public Creater loginService(String aemail, String apassword) {
        Creater creater = daoCreater.findCreaterByCphoneAndCpassword(aemail, apassword);
        return creater;
    }

    //实现注册业务逻辑
    @Override
    public Creater registService(Creater creater) {
        if (daoCreater.findCreaterByCphone(creater.getCphone()) != null) {
            //已经有用户了，不可以注册
            return null;
        } else {
            Creater newcreater = daoCreater.save(creater);
            if (newcreater != null) {
                creater.setCpassword("");
            }
            return newcreater;
        }
    }

    //实现修改密码业务逻辑
    @Override
    public Creater updatepasswordService(String cphone, String cpassword) {
        Creater creater = daoCreater.findCreaterByCphone(cphone);
        if (creater != null) {
            creater.setCpassword(cpassword);
            return daoCreater.save(creater);
        } else {
            return null;
        }
    }

    //实现返回所有视频信息业务逻辑
    @Override
    public List<Creater> findallService() {
        return daoCreater.findAll();
    }

    //实现返回一个合作单号下的UP主业务逻辑
    @Override
    public Creater findbycoidService(int coid){
        Sign sign=daoSign.findByCoid(coid);
        if(sign==null){return null;}
        return daoCreater.findByCid(sign.getCid());
    }
    @Override
    //实现返回一个视频创作者的所有信息
    public Creater findbycidService(int cid){
        Creater creater=daoCreater.findByCid(cid);
        if (creater==null){
            return null;
        }
        else
            return creater;
    }
}

